package com.springcloud.zuul.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.springcloud.zuul.common.constant.Constant;
import com.springcloud.zuul.common.constant.ServiceCode;
import com.springcloud.zuul.common.utlis.JsonResult;
import com.springcloud.zuul.common.utlis.MapUtils;
import com.springcloud.zuul.common.utlis.listToTreeUtils;
import com.springcloud.zuul.entity.SysMenuEntity;
import com.springcloud.zuul.entity.SysMenusEntity;
import com.springcloud.zuul.mapper.SysMenuDao;
import com.springcloud.zuul.service.SysConfigService;
import com.springcloud.zuul.service.SysMenuService;
import com.springcloud.zuul.service.SysRoleMenuService;
import com.springcloud.zuul.service.SysUserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;


@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenuEntity> implements SysMenuService {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;

	@Autowired
	private SysMenuDao sysMenuDao;

	@Autowired
	SysConfigService sysConfigService;
	
	@Override
	public List<SysMenuEntity> queryListParentId(String parentId, List<String> menuIdList,String productId) {
		List<SysMenuEntity> menuList = queryListParentId(parentId,productId);
		if(menuIdList == null){
			return menuList;
		}
		
		List<SysMenuEntity> userMenuList = new ArrayList<>();
		for(SysMenuEntity menu : menuList){
			if(menuIdList.contains(menu.getMenuId())){
				userMenuList.add(menu);
			}
		}
		return userMenuList;
	}

	/**
	 * Description:根据父菜单，查询子菜单
	 * @author zhlu
	 * @date 2019/12/28 2:02 下午
	 * @param parentId
	 * @return java.util.List<com.springcloud.zuul.entity.SysMenuEntity>
	 */
	@Override
	public List<SysMenuEntity> queryListParentId(String parentId,String productId) {
		return baseMapper.queryListParentId(parentId,productId);
	}

	@Override
	public List<SysMenuEntity> queryNotButtonList() {
		return baseMapper.queryNotButtonList();
	}

	@Override
	public List<SysMenuEntity> getUserMenuList(String userId,String productId) {
		//手动设置超级管理员,拥有最高权限
		List<String> list = Arrays.asList(sysConfigService.getValue("superAdmin").split(","));
		//系统管理员，拥有最高权限
		if(list.contains(userId)){
			return getAllMenuList(null,productId);
		}
		//用户菜单列表
		List<String> menuIdList = sysUserService.queryAllMenuId(userId,productId);
		return getAllMenuList(menuIdList,productId);
	}

	@Override
	public void delete(String menuId){
		//删除菜单
		this.removeByMap(new MapUtils().put("menu_id", menuId));
		//删除菜单与角色关联
//		sysRoleMenuService.removeByMap(new MapUtils().put("menu_id", menuId));
	}

	/**
	 * 获取所有菜单列表
	 */
	private List<SysMenuEntity> getAllMenuList(List<String> menuIdList,String productId){
		//查询根菜单列表
		List<SysMenuEntity> menuList = queryListParentId("0", menuIdList,productId);
		//递归获取子菜单
		getMenuTreeList(menuList, menuIdList,productId);
		
		return menuList;
	}

	/**
	 * 递归
	 */
	private List<SysMenuEntity> getMenuTreeList(List<SysMenuEntity> menuList, List<String> menuIdList,String productId){
		List<SysMenuEntity> subMenuList = new ArrayList<SysMenuEntity>();
		
		for(SysMenuEntity entity : menuList){
			//目录
			if(entity.getType() == Constant.MenuType.CATALOG.getValue()){
				entity.setList(getMenuTreeList(queryListParentId(entity.getMenuId(), menuIdList,productId), menuIdList,productId));
			}
			subMenuList.add(entity);
		}
		
		return subMenuList;
	}

	/**
	 * Description:根据菜单编码获取菜单资料
	 * @author zhlu
	 * @date 2019/12/28 10:30 上午
	 * @param menuId
	 * @return com.springcloud.zuul.entity.SysMenuEntity
	 */
	@Override
	public SysMenuEntity queryByMenuId(String menuId){
		return sysMenuDao.queryByMenuId(menuId);
	}

	/**
	 * Description:根据菜单编码获取是否存在当前编码
	 * @author zhlu
	 * @date 2019/12/28 10:48 上午
	 * @param
	 * @return int
	 */
	@Override
	public int countByMenuId(String menuId){
		return sysMenuDao.countByMenuId(menuId);
	}

	/**
	 * Description:获取所有的菜单
	 * @author zhlu
	 * @date 2019/12/30 11:53 上午
	 * @param
	 * @return com.springcloud.zuul.common.utlis.JsonResult
	 */
	@Override
	public JsonResult getMenuList(){
		//获取所有的菜单
		List<SysMenuEntity> menuList = this.list();
		for(SysMenuEntity sysMenuEntity : menuList){
			SysMenuEntity parentMenuEntity = sysMenuDao.queryByMenuId(sysMenuEntity.getParentId());
			if(parentMenuEntity != null){
				sysMenuEntity.setParentName(parentMenuEntity.getMenuName());
			}
		}
//		return JsonResult.success(listToTreeUtils.buidTree(JSON.parseArray(JSON.toJSONString(menuList)),"menuId","parentId","children"));
		return JsonResult.success(menuList);
	}

	/**
	 * Description:获取相关产品、公司、角色菜单权限列表
	 * @author zhlu
	 * @date 2020/1/2 4:00 下午
	 * @param menusEntity
	 * @return com.springcloud.zuul.common.utlis.JsonResult
	 */
	@Override
	public  JsonResult queryMenusList(SysMenusEntity menusEntity){
		//获取所有的菜单
		List<SysMenuEntity> menuList = this.list();
		for(SysMenuEntity sysMenuEntity : menuList){
			SysMenuEntity parentMenuEntity = sysMenuDao.queryByMenuId(sysMenuEntity.getParentId());
			if(parentMenuEntity != null){
				sysMenuEntity.setParentName(parentMenuEntity.getMenuName());
			}
		}

		//获取产品下的权限功能列表
		List<SysMenuEntity>productList=sysMenuDao.queryProductList(menusEntity.getProductId());
		//禁用产品之外的权限
		for(SysMenuEntity menuEntity:menuList){
			menuEntity.setOpen(false);
			for(SysMenuEntity menuEntity1:productList){
				//打开
				if(menuEntity.getMenuId().equals(menuEntity1.getMenuId())){
					menuEntity.setOpen(true);
					break;
				}
			}
		}
//		return JsonResult.success(listToTreeUtils.buidTree(JSON.parseArray(JSON.toJSONString(menuList)),"menuId","parentId","children"));
		return JsonResult.success(menuList);
	}

	/**
	 * Description:获取公司与产品的权限功能列表
	 * @author zhlu
	 * @date 2020/1/7 2:42 下午
	 * @param deptId
	 * @param produList
	 * @return java.util.List<java.lang.String>
	 */
	@Override
	public List<String>queryDeptAndMenuList(String deptId,List<String> produList){
		return sysMenuDao.queryDeptAndMenuList(deptId,produList);
	}

	/**
	 * Description:根据产品编码。获取当前产品的权限列表
	 * @author zhlu
	 * @date 2020/1/7 2:42 下午
	 * @param produList
	 * @return java.util.List<java.lang.String>
	 */
	@Override
	public List<String>queryProductIdList(List<String> produList){
		return sysMenuDao.queryProductIdList(produList);
	}
}
